<!--登录页-->
<template>
  <div>
    <el-container>
      <img src="/img/login.png" style="width: 100%;height: 100vh;position: relative" alt="">
      <el-card style="position: absolute;top:30%;right: 10%;font-size: 30px">
        <h3>智慧工地检查平台</h3>
        <el-form label-position="top">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input placeholder="请输入密码" show-password v-model="user.password"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" style="width: 100%;" @click="login">登录</el-button>
        <div style="font-size: 14px;color:#666;text-align: center;padding: 10px 0;">
          (体验账号:tom 密码:123456)
        </div>
      </el-card>
    </el-container>
  </div>
</template>

<script setup>
import {ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";
import router from "@/router";

//定义收集用户数据的方法
const user = ref({username:'',password:''});

//定义登录的方法
const login = ()=>{
  console.log(user.value);
  let data = qs.stringify(user.value);
  axios.post(BASE_URL+'/v1/user/login', data)
      .then((response) => {
        //回调函数:服务器响应后执行的代码
        //response是响应对象,我们可以通过response.data得到服务器的响应数据
        console.log(response.data);//后端返回的JsonResult
        if (response.data.code == 2000) {
          ElMessage.success('登录成功!');
          router.push('/user');
          //登录成功后将后端返回的userVO存储在localStorage中
          console.log(response.data.data);//是JsonResult的data值,此处是userVO
          console.log(JSON.stringify(response.data.data));
          localStorage.setItem('user', JSON.stringify(response.data.data));
        } else {
          ElMessage.error(response.data.msg);
          user.value = {username: '', password: ''};
        }
      })
}
</script>



<style scoped>

</style>